package jmu.edu.yjc.service.answer;

import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class QuestionParser {
    private static List<String> diseaseQuestionType = Arrays.asList("disease_symptom","disease_cause","disease_acompany",
            "disease_not_food","disease_do_food","disease_drug","disease_check","disease_prevent","disease_lasttime",
            "disease_cureway","disease_cureprob","disease_easyget","disease_desc");
    /**
     * 这个方法反过来把传过来的字典的key当value
     * value当key
     * @Auther yjc
     * @param dict
     * @return Map<String, List<String>>
     */
    private Map<String, List<String>> buildEntityDict(Map<String,String> dict){
        Map<String,List<String>> entity_dict = new HashMap<>();
        for (Map.Entry<String, String> entry : dict.entrySet()) {
            if(entity_dict.containsKey(entry.getValue())){
                entity_dict.get(entry.getValue()).add(entry.getKey());
            }else {
                List<String> entity = new ArrayList<>();
                entity.add(entry.getKey());
                entity_dict.put(entry.getValue(),entity);
            }
        }
        return entity_dict;
    }

    /***
     * @description: 这里对传过来的问题类型做处理返回对应的sql
     * @param: classifierResult
     * @return: Map<String,List<String>>
     * @author yjc
     * @date: 2023/3/11 23:58
     */
    public Map<String,List<String>> getSearchSql(ClassifierResult classifierResult){
        Map<String, List<String>> entityDict = buildEntityDict(classifierResult.getDict());
        List<String> questionTypes = classifierResult.getQuestionTypes();
        Map<String,List<String>> sqls = new HashMap<>();
        for (String questionType : questionTypes) {
            List<String> sql = new ArrayList<>();
            if(diseaseQuestionType.contains(questionType)) {
                sql = sql(questionType, entityDict.get("disease"));
            }else if(questionType.equals("symptom_disease")){
                sql = sql(questionType, entityDict.get("symptom"));
            }else if(questionType.equals("food_not_disease")){
                sql = sql(questionType, entityDict.get("food"));
            }else if(questionType.equals("food_do_disease")){
                sql = sql(questionType, entityDict.get("food"));
            }else if(questionType.equals("drug_disease")){
                sql = sql(questionType, entityDict.get("drug"));
            }else if(questionType.equals("check_disease")){
                sql = sql(questionType, entityDict.get("check"));
            }
            if(sql.size()>=0){
                sqls.put(questionType,sql);
            }
        }
        return sqls;
    }
    private List<String> sql(String questionType , List<String> entities){
        if(entities.size()<=0){
            return null;
        }
        List<String> sql = new ArrayList<>();
        if(questionType.equals("disease_cause")){
            //查询疾病的原因
            entities.forEach(entity -> sql.add("MATCH (m:disease) where m.name = '" + entity + "' return m.name, m.cause"));
        }else if(questionType.equals("disease_prevent")){
            //查询疾病的防御措施
            entities.forEach(entity -> sql.add("MATCH (m:disease) where m.name = '" + entity + "' return m.name, m.prevent"));
        }else if(questionType.equals("disease_lasttime")){
            //查询疾病的持续时间
            entities.forEach(entity -> sql.add("MATCH (m:disease) where m.name = '" + entity + "' return m.name, m.cureLasttime"));
        }else if(questionType.equals("disease_cureprob")){
            //查询疾病的治愈概率
            entities.forEach(entity -> sql.add("MATCH (m:disease) where m.name = '" + entity + "' return m.name, m.curedProb"));
        }else if(questionType.equals("disease_cureway")){
            //查询疾病的治疗方式
            entities.forEach(entity -> sql.add("MATCH (m:disease) where m.name = '" + entity + "' return m.name, m.cureWay"));
        }else if(questionType.equals("disease_easyget")){
            //查询疾病的易发人群
            entities.forEach(entity -> sql.add("MATCH (m:disease) where m.name = '" + entity + "' return m.name, m.easyGet"));
        }else if(questionType.equals("disease_desc")){
            //查询疾病的相关介绍
            entities.forEach(entity -> sql.add("MATCH (m:disease) where m.name = '" + entity + "' return m.name, m.desc"));
        }else if(questionType.equals("disease_symptom")){
            //查询疾病有哪些症状
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:has_symptom]->(n:symptom) where m.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("symptom_disease")){
            //查询症状可能是哪些疾病
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:has_symptom]->(n:symptom) where n.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("disease_acompany")){
            //查询疾病的并发症
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:complication_with]->(n:acompany) where m.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("disease_not_food")){
            //查询疾病的忌口
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:no_eat]->(n:food) where m.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("disease_do_food")){
            //查询疾病建议吃的东西
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:do_eat]->(n:food) where m.name = '" + entity + "' return m.name, r.name, n.name"));
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:recommend_food]->(n:food) where m.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("food_not_disease")){
            //已知忌口查疾病
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:no_eat]->(n:food) where n.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("food_do_disease")){
            //已知推荐食物查疾病
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:do_eat]->(n:food) where n.name = '" + entity + "' return m.name, r.name, n.name"));
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:recommend_food]->(n:food) where n.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("disease_drug")){
            //查询疾病常用药品－药品别名记得扩充
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:common_drug]->(n:drug) where m.name = '" + entity + "' return m.name, r.name, n.name"));
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:recommend_drug]->(n:drug) where m.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("drug_disease")){
            //已知药品查询能够治疗的疾病
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:common_drug]->(n:drug) where n.name = '" + entity + "' return m.name, r.name, n.name"));
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:recommend_drug]->(n:drug) where n.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("disease_check")){
            //查询疾病应该进行的检查
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:need_check]->(n:check) where m.name = '" + entity + "' return m.name, r.name, n.name"));
        }else if(questionType.equals("check_disease")){
            //已知检查查询疾病
            entities.forEach(entity -> sql.add("MATCH (m:disease)-[r:need_check]->(n:check) where n.name = '" + entity + "' return m.name, r.name, n.name"));
        }
        return sql;
    }
}
